<template>

  <!--element ui 开发登录界面-->
  <!--中间对齐方式-->
  <el-row type="flex" class="row-bg" justify="center">

    <el-col :xl="6" :lg="7">
      <h2>欢迎来到VueAdmin管理系统</h2>
      <el-image :src="require('@/assets/img/login-logo.png')"></el-image>
      <p>作者hzj</p>
    </el-col>

    <el-col :span="1">
      <el-divider direction="vertical"></el-divider>
    </el-col>

    <el-col :xl="6" :lg="7">
      <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="用户名" prop="username" >
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password"></el-input>
        </el-form-item>

        <el-form-item label="验证码" prop="code">
          <el-input v-model="loginForm.code" style="width: 170px;float: left"></el-input>
          <el-image :src="captchaImg" @click="getCaptcha"></el-image>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')">立即创建</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>

      </el-form>
    </el-col>
  </el-row>

</template>

<script>

  import {loginrequest , getCaptchaimg} from '../network/request'

  /*方法区*/
  const methods = {

    submitForm(FormName) {
      this.$refs[FormName].validate((valid) => {
        if (valid) {

          loginrequest(this.loginForm).then(res => {
            console.log(res)
            const jwt = res.headers['authorization']

            this.$store.commit("SET_TOKEN" , jwt)

            this.$router.push('/index')
          }).catch(err => {
            console.log(err);
          })

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(FormName) {
      this.$refs[FormName].resetFields();
    },
    getCaptcha() {
      /*this.$axios.get('/captcha').then(result => {
        console.log(result)
        this.loginForm.token = result.data.data.token;
        this.captchaImg = result.data.data.captchaImg
      })*/

      getCaptchaimg().then(result => {
        this.loginForm.token = result.data.data.token;
        this.captchaImg = result.data.data.captchaImg
        this.loginForm.code = ''
      })

    },
  }



  export default {
    name: "Login",
    data() {
      return {
        loginForm: {
          username: '',
          password: '',
          code: '',
          token: ''
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '请输入验证码', trigger: 'blur' },
            { min: 5, max: 5, message: '长度 5 个字符', trigger: 'blur' }
          ]
        },
        captchaImg: null
      };
    },
    methods,
    created(){
      this.getCaptcha()
    }
  }


</script>

<style >

@import "../assets/css/Login.css";

</style>
